package com.eagledata.project.tool.upload;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.fastjson.JSONObject;
import com.eagledata.common.utils.DateUtils;
import com.eagledata.common.utils.FileUploadUtils;

@Controller
@RequestMapping("/upload")
public class UpLoadController {

	@RequestMapping("")
	public String upLoadImage(HttpServletRequest request,HttpServletResponse response, @RequestParam("file") MultipartFile[] file) {
		
		if(file!=null&&file.length>0){
            //组合image名称，“;隔开”
            List<String> fileName =new ArrayList<String>();

            try {
                for (int i = 0; i < file.length; i++) {
                    if (!file[i].isEmpty()) {
                        //上传文件，随机名称，";"分号隔开
                        fileName.add(FileUploadUtils.uploadImage(request, "/upload/" + DateUtils.parseDateToStr("yyyy-MM-dd", new Date())+"/", file[i], true));
                    }
                }
                //上传成功
                if(fileName!=null&&fileName.size()>0){
                    return JSONObject.toJSONString("上传成功" + fileName);
                }else {
                    return "上传失败！文件格式错误！";
                }
            } catch (Exception e) {
                e.printStackTrace();
                return "上传出现异常";
            }
        }else {
            return "没有检测到文件！";
        }
	}
}
